package com.youlu.campus.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityCmsConfig;
import com.youlu.campus.entity.ActivityIconConfig;
import com.youlu.campus.service.activity.ActivityCmsConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Slf4j
@Service
public class ActivityCmsConfigServiceImpl implements ActivityCmsConfigService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ActivityCmsConfig findBy(String activityId) {
        log.info(":>>> 开始获取活动内容限制配置:{}", activityId);
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        ActivityCmsConfig activityCmsConfig = mongoTemplate.findOne(query, ActivityCmsConfig.class);
        if(Objects.isNull(activityCmsConfig)){
            return null;
        }
        List<ActivityIconConfig> h5Menus = activityCmsConfig.getH5Menus().stream().sorted(Comparator.comparing(ActivityIconConfig::getOrderNo)).collect(Collectors.toList());
        activityCmsConfig.setH5Menus(h5Menus);
        log.info(":>>> 活动显示配置:{}", JSON.toJSON(activityCmsConfig));
        return activityCmsConfig;
    }

    @Override
    public void save(ActivityCmsConfig activityCmsConfig) {
        if(Objects.isNull(activityCmsConfig)){
            return;
        }
        String activityId = activityCmsConfig.getActivityId();
        if(StringUtils.isBlank(activityId)){
            throw new BusinessException("活动ID不能为空");
        }
        Date now = new Date();
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        ActivityCmsConfig activityCmsConfigExist = this.mongoTemplate.findOne(query, ActivityCmsConfig.class);
        if(Objects.isNull(activityCmsConfigExist)){
            activityCmsConfigExist = new ActivityCmsConfig();
            activityCmsConfig.setCreatedTime(now);
            BeanUtils.copyProperties(activityCmsConfig,activityCmsConfigExist);
            activityCmsConfigExist.setId(null);
        }else{
            activityCmsConfig.setUpdatedTime(now);
            BeanUtils.copyProperties(activityCmsConfig,activityCmsConfigExist);
        }
        List<ActivityIconConfig> h5Menus = activityCmsConfig.getH5Menus();
        if(CollectionUtils.isEmpty(h5Menus)){
            this.mongoTemplate.remove(query,ActivityCmsConfig.class);
            return;
        }
        this.mongoTemplate.save(activityCmsConfigExist);
    }
}
